package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.SelectVo;
import com.zb.entity.ZjWorkWarning;
import com.zb.mapper.ZjWorkWarningMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjWorkWarningService;
import com.zb.vo.WorkWarningListVo;
import com.zb.vo.WorkWarningVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 工单预警表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-09-13
 */
@Service
public class ZjWorkWarningServiceImpl extends ServiceImpl<ZjWorkWarningMapper, ZjWorkWarning> implements ZjWorkWarningService {
    @Resource
    private ZjWorkWarningMapper workWarningMapper;

    // 新增或修改工单预警
    @Override
    public void save(Long custAccId, WorkWarningVo workWarningVo) {
        Assert.isTrue(null == workWarningMapper.existByMin(custAccId, workWarningVo.getId(), workWarningVo.getMin()), "预警名称-最小值[" + workWarningVo.getMin() + "]已重复");
        Assert.isTrue(null == workWarningMapper.existByMax(custAccId, workWarningVo.getId(), workWarningVo.getMax()), "预警名称-最大值[" + workWarningVo.getMax() + "]已重复");
        ZjWorkWarning warning = new ZjWorkWarning();
        BeanUtils.copyProperties(workWarningVo, warning);
        warning.setDisabled(workWarningVo.getStatus());
        if (null != warning.getId()) {
            this.updateById(warning);
            return;
        }
        warning.setId(IdWorker.getId());
        warning.setCustAccId(custAccId);
        this.save(warning);
    }

    // 获取工单预警列表
    @Override
    public Map<String, Object> getList(Long custAccId, SelectVo selectVo) {
        PageUtil.setPageLimit(selectVo);
        List<ZjWorkWarning> warningList = workWarningMapper.getList(custAccId, selectVo);
        List<WorkWarningListVo> voList = warningList.stream().map(warning -> {
            WorkWarningListVo vo = new WorkWarningListVo();
            BeanUtils.copyProperties(warning, vo);
            vo.setStatus(warning.getDisabled());
            vo.setCreateTime(warning.getGmtCreate());
            vo.setUpdateTime(warning.getGmtUpdate());
            return vo;
        }).toList();
        return PageUtil.pageList(selectVo.getPage(), selectVo.getLimit(), workWarningMapper.getListCount(custAccId, selectVo), voList);
    }

    // 根据预警ID获取工单预警详情
    @Override
    public WorkWarningVo getDetail(Long id) {
        ZjWorkWarning warning = this.getById(id);
        Assert.isTrue(null != warning, "预警参数[" + id + "]的数据不存在");

        WorkWarningVo vo = new WorkWarningVo();
        BeanUtils.copyProperties(warning, vo);
        vo.setStatus(warning.getDisabled());
        return vo;
    }

    // 根据预警ID批量启用或禁用预警
    @Override
    public void updateDisabledBatchById(List<Long> idList, boolean status) {
        workWarningMapper.updateDisabledBatchById(idList, status);
    }

    // 根据预警ID批量删除预警
    @Override
    public void deleteBatchById(List<Long> idList) {
        if (null == idList || idList.isEmpty()) {
            return;
        }
        this.removeByIds(idList);
    }

    @Override
    public List<ZjWorkWarning> getByCustAccIdAndDay(Long custAccId, List<Long> warningDays) {
        if (null == warningDays || warningDays.isEmpty()) {
            return new ArrayList<>();
        }
        return workWarningMapper.getByCustAccIdAndDay(custAccId, warningDays);
    }
}
